package com.z.front;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.z.admin.dao.NoticeMapper;
import com.z.admin.dao.UserMapper;
import com.z.admin.entity.*;
import com.z.admin.service.*;
import com.z.common.base.PageTableRequest;
import com.z.common.base.ResponseCode;
import com.z.common.base.Results;
import com.z.common.utils.ImageBase64Converter;
import com.z.common.utils.MarkdownUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/f")
public class FrontController {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private ICarouselService carouselService;
    @Autowired
    private INoticeService noticeService;

    @Autowired
    private ITagService tagService;
    @Autowired
    private ITypeService typeService;

    @Autowired
    private IBlogService blogService;
    @Autowired
    private ICommentService commentService;

    @PostMapping("/types/list")
    @ResponseBody
    public Results list() {
        try{
            Map<String, Object> map = new HashMap<>();
            List<Type> list = typeService.getBaseMapper().selectByMap(map);
            return Results.success(list.size(), list);
        }catch (Exception e ){
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
    }

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        model.addAttribute("comments", commentService.listCommentByBlogId(blogId));
        return "front/blog/blogDetail :: commentList";
    }
    @PostMapping("/comments")
    public String post(Comment comment, HttpSession session) {
        if(StringUtils.isNotBlank(comment.getContent())){
            comment.setParentCommentId(comment.getParentComment().getId());
            comment.setAvatar("/img/avatar.png");
            comment.setAdminComment(false);
            commentService.saveComment(comment);
        }
        return "redirect:/f/comments/" + comment.getBlogId();
    }
    @GetMapping("types/{id}")
    public String type(PageTableRequest page, Model model,@PathVariable Long id) {
        page.countOffset();
        List<Type> types = typeService.getBaseMapper().selectByMap(new HashMap<>());
        if(id == -1 && types.size() != 0){
            id = types.get(0).getId();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("type_Id/eq/s", id);
        Results<Blog> blogs = blogService.findPageByMap(map, 0, 10);
        model.addAttribute("types", types);
        model.addAttribute("blogs", blogs);
        model.addAttribute("activeBtn", id);
        return "front/blog/types";
    }

    @GetMapping("carousel/list")
    @ApiOperation("轮播图查询")
    @ResponseBody
    public Results list(PageTableRequest page, HttpServletRequest request) {
        page.countOffset();

        Map<String, Object> map = new HashMap<>();
        map.put("active_eq_s", "1");
        Results<List<Carousel>> results = carouselService.findPageByMap(map, page.getOffset(), 5);
        List<Carousel> list = (List<Carousel>) results.getData();
        for(Carousel c :list){
            c.setUrl("data:image/png;base64," + ImageBase64Converter.convertFileToBase64(c.getUrl()));
        }
        results.setData(list);
        return results;
    }

    @GetMapping("notice/list")
    @ApiOperation("公告查询")
    @ResponseBody
    public Results noticeList(PageTableRequest page, HttpServletRequest request) {
        page.countOffset();

        Map<String, Object> map = new HashMap<>();
        map.put("published_eq_s", "1");
        Results<List<Notice>> results = noticeService.findPageByMap(map, page.getOffset(), 5);
        return results;
    }

    @GetMapping("notice/show")
    @ApiOperation("公告展示页面")
    public String show(Model model, String id) {
        Notice notice = noticeMapper.selectById(id);
        String content = notice.getContent();
        notice.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        notice.setFirstPicture("data:image/png;base64," + ImageBase64Converter.convertFileToBase64(notice.getFirstPicture()));
        model.addAttribute("notice", notice);
        User user = userMapper.selectById(notice.getUserId());
        model.addAttribute("user", user == null ? new User() : user);
        return "front/notice/noticeDetail";
    }


}
